package com.ifnk.portaljava.hlyt.dao.impl;

import com.ifnk.portaljava.dapWeb.advance.DBAccess;
import com.ifnk.portaljava.hlyt.beans.AdvCond;
import com.ifnk.portaljava.hlyt.beans.User;
import com.ifnk.portaljava.hlyt.dao.IAdvCondDAO;
import flex.messaging.FlexContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 * <p>Title:高级查询条件DAO类 </p>
 *
 * <p>Description: 高级查询条件的增删改查</p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 * 
 * @author chengzhang.shi.hlyt@gmail.com
 * @version 1.0
 */
public class AdvCondDAOImpl implements IAdvCondDAO {

	public List<AdvCond> getAdvConds() {
		Connection conn = DBAccess.getConn();
		Log loger = LogFactory.getLog(AdvCondDAOImpl.class);
		Statement statement;
		String sql;
		List<AdvCond> advConds = new ArrayList<AdvCond>();
		try {
			statement = conn.createStatement();
			String tableName = "advcond";
			sql = "select id, user_id, cond_name, cond_content from " + tableName + " where user_id = " + 
					((User)FlexContext.getHttpRequest().getSession().getAttribute("user")).getId();
			ResultSet rs =  statement.executeQuery(sql);
			
			while(rs.next()) {
				AdvCond advCond = new AdvCond();
				advCond.setId(rs.getInt(1));
				advCond.setUserId(rs.getInt(2));
				advCond.setCondName(rs.getString(3));
				advCond.setCondContent(rs.getString(4));
				advConds.add(advCond);
			}
			
//			loger.info("导入数据库成功！数据为：" + data);
		} catch (SQLException e) {
			loger.error("查询advcond异常：" + e);
		} finally {
			DBAccess.freeConnection(conn);
		}
		return advConds;
	}

	public int insertAdvCond(AdvCond advCond) {
		Connection conn = DBAccess.getConn();
		Log loger = LogFactory.getLog(AdvCondDAOImpl.class);
		Statement statement;
		String sql = "";
		try {
			statement = conn.createStatement();
			String tableName = "advcond";
			sql = "insert into " + tableName + " (user_id, cond_name, cond_content, cond_createtime) values (" + 
					advCond.getUserId() + ", \"" + advCond.getCondName() + "\", \"" + advCond.getCondContent().replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\", now())";
//					((User)FlexContext.getHttpRequest().getSession().getAttribute("user")).getId();
			statement.executeUpdate(sql);
			return 1;
		} catch (SQLException e) {
			loger.error("插入advcond异常：" + e + "\n" + sql);
		} finally {
			DBAccess.freeConnection(conn);
		}
		return 0;
	}
	
	public int deleteAdvCond(AdvCond advCond) {
		Connection conn = DBAccess.getConn();
		Log loger = LogFactory.getLog(AdvCondDAOImpl.class);
		Statement statement;
		String sql = "";
		int result = -1;
		try {
			statement = conn.createStatement();
			String tableName = "advcond";
			sql = "delete from " + tableName + " where id = " + advCond.getId();
			statement.executeUpdate(sql);
			result = advCond.getId();
		} catch (SQLException e) {
			loger.error("删除advcond异常：" + e + "\n" + sql);
		} finally {
			DBAccess.freeConnection(conn);
		}
		return result;
	}
	
	public int updateAdvCond(AdvCond advCond) {
		Connection conn = DBAccess.getConn();
		Log loger = LogFactory.getLog(AdvCondDAOImpl.class);
		Statement statement;
		String sql = "";
		int result = -1;
		try {
			statement = conn.createStatement();
			String tableName = "advcond";
			sql = "update " + tableName + " set cond_name = \"" + advCond.getCondName() + "\", " +
						"cond_content = \"" + advCond.getCondContent().replaceAll("\"", Matcher.quoteReplacement("\\\"")) + 
						"\", cond_createtime = now() where id = " + advCond.getId();
			statement.executeUpdate(sql);
			result = advCond.getId();
		} catch (SQLException e) {
			loger.error("更新advcond异常：" + e + "\n" + sql);
		} finally {
			DBAccess.freeConnection(conn);
		}
		return result;
	}
	
}
